/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUI.java
 *
 * Created on Nov 7, 2011, 6:58:36 PM
 */
package TheBeatGoesOn.GUI;

import babydisco.ICA.FastICA;
import babydisco.Math.Fourier;
import babydisco.Math.Morphology;
import babydisco.Math.SVD.SVDICA;
import babydisco.Math.Wavelet;
import babydisco.NN.DelayedNetwork;
import babydisco.NN.ElmanNetwork;
import babydisco.NN.Trainer;
import babydisco.util.DataReader;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import org.encog.neural.networks.BasicNetwork;
import org.encog.persist.EncogDirectoryPersistence;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *
 * @author rik_claessens
 */
public class GUI extends javax.swing.JFrame {

    private JPanel myGraph;
    private File currentFile;
    private XYSeriesCollection showSignals, showFourierSignals, showWaveletSignals, showSVDICASignals;
    private Double[] allthorax_1, allthorax_2, allabdomen_1, allabdomen_2, allabdomen_3, allannotations, alltimes;
    private Double[] allFourierthorax_1, allFourierthorax_2, allFourierabdomen_1, allFourierabdomen_2, allFourierabdomen_3, allFourierannotations, allFourierfrequencies;
    private Double[] allWaveletthorax_1, allWaveletthorax_2, allWaveletabdomen_1, allWaveletabdomen_2, allWaveletabdomen_3, allWaveletannotations, allWavelettimes;
    private double[][] allSVDthorax_1, allSVDthorax_2, allSVDabdomen_1, allSVDabdomen_2, allSVDabdomen_3;


    private DelayedNetwork delayedNetwork;
    private ElmanNetwork elmanNetwork;
    private ArrayList<Double>[] fakeData;
    private Double[] allElmanIdeal, allElmanOriginal, allElmanFake, allDelayedIdeal, allDelayedOriginal,allDelayedFake, allFakeTimes;
    private Double[] allNNthorax_1, allNNthorax_2, allNNabdomen_1, allNNabdomen_2, allNNabdomen_3;
    private XYSeriesCollection showNNSignals;

    /** Creates new form GUI */
    public GUI() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        controlPanel = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        loadFakeDataButton = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        maxTextfield = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        minTextfield = new javax.swing.JTextField();
        abdomen_3_checkbox = new javax.swing.JCheckBox();
        abdomen_2_checkbox = new javax.swing.JCheckBox();
        abdomen_1_checkbox = new javax.swing.JCheckBox();
        thorax_2_checkbox = new javax.swing.JCheckBox();
        thorax_1_checkbox = new javax.swing.JCheckBox();
        jButton4 = new javax.swing.JButton();
        fourierControlPanel = new javax.swing.JPanel();
        jButton5 = new javax.swing.JButton();
        fourierControlPanel1 = new javax.swing.JPanel();
        jButton6 = new javax.swing.JButton();
        fourierControlPanel2 = new javax.swing.JPanel();
        jButton7 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        icaComponentField = new javax.swing.JTextField();
        fourierIcaCheckbox = new javax.swing.JCheckBox();
        jPanel1 = new javax.swing.JPanel();
        jButton8 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jButton3 = new javax.swing.JButton();
        loadDelayedNetworkButton = new javax.swing.JButton();
        loadElmanNetworkButton = new javax.swing.JButton();
        loadComputeFakeDataButton = new javax.swing.JButton();
        computeRealData = new javax.swing.JButton();
        ecgdatatab = new javax.swing.JTabbedPane();
        graphPanel = new javax.swing.JPanel();
        fourierPanel = new javax.swing.JPanel();
        waveletPanel = new javax.swing.JPanel();
        icaTab = new javax.swing.JPanel();
        SVDpanel = new javax.swing.JPanel();
        nnPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        controlPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Controls"));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Files"));

        jButton1.setText("Load File");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        loadFakeDataButton.setText("Load Fake Data");
        loadFakeDataButton.setActionCommand("Load Fake Data");
        loadFakeDataButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadFakeDataButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jButton1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(loadFakeDataButton))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                .add(jButton1)
                .add(loadFakeDataButton))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Graph"));

        jButton2.setText("Reload");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        maxTextfield.setText("60");

        jLabel2.setText("max:");

        jLabel1.setText("min:");

        minTextfield.setText("0");

        abdomen_3_checkbox.setSelected(true);
        abdomen_3_checkbox.setText("Abdomen_3");

        abdomen_2_checkbox.setSelected(true);
        abdomen_2_checkbox.setText("Abdomen_2");

        abdomen_1_checkbox.setSelected(true);
        abdomen_1_checkbox.setText("Abdomen_1");

        thorax_2_checkbox.setSelected(true);
        thorax_2_checkbox.setText("Thorax_2");

        thorax_1_checkbox.setSelected(true);
        thorax_1_checkbox.setText("Thorax_1");

        jButton4.setText("Restore");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jButton2)
                    .add(thorax_1_checkbox)
                    .add(thorax_2_checkbox)
                    .add(abdomen_1_checkbox)
                    .add(abdomen_2_checkbox)
                    .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel3Layout.createSequentialGroup()
                            .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(jLabel1)
                                .add(jLabel2))
                            .add(10, 10, 10)
                            .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(maxTextfield, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 84, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(minTextfield, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)))
                        .add(org.jdesktop.layout.GroupLayout.LEADING, abdomen_3_checkbox)))
                .add(35, 35, 35)
                .add(jButton4))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton2)
                    .add(jButton4))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(thorax_1_checkbox)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(thorax_2_checkbox)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(abdomen_1_checkbox)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(abdomen_2_checkbox)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(abdomen_3_checkbox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 23, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(11, 11, 11)
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(minTextfield, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(26, 26, 26)
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(maxTextfield, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(32, 32, 32))
        );

        fourierControlPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Fourier"));

        jButton5.setText("Fourier Transform");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout fourierControlPanelLayout = new org.jdesktop.layout.GroupLayout(fourierControlPanel);
        fourierControlPanel.setLayout(fourierControlPanelLayout);
        fourierControlPanelLayout.setHorizontalGroup(
            fourierControlPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(fourierControlPanelLayout.createSequentialGroup()
                .add(jButton5)
                .addContainerGap(210, Short.MAX_VALUE))
        );
        fourierControlPanelLayout.setVerticalGroup(
            fourierControlPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jButton5)
        );

        fourierControlPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Wavelet"));

        jButton6.setText("Wavelet Transform");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout fourierControlPanel1Layout = new org.jdesktop.layout.GroupLayout(fourierControlPanel1);
        fourierControlPanel1.setLayout(fourierControlPanel1Layout);
        fourierControlPanel1Layout.setHorizontalGroup(
            fourierControlPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(fourierControlPanel1Layout.createSequentialGroup()
                .add(jButton6)
                .addContainerGap(204, Short.MAX_VALUE))
        );
        fourierControlPanel1Layout.setVerticalGroup(
            fourierControlPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jButton6)
        );

        fourierControlPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Fast ICA"));

        jButton7.setText("Fast ICA");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jLabel3.setText("Components:");

        icaComponentField.setText("2");
        icaComponentField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                icaComponentFieldActionPerformed(evt);
            }
        });

        fourierIcaCheckbox.setText("Fourier");
        fourierIcaCheckbox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fourierIcaCheckboxActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout fourierControlPanel2Layout = new org.jdesktop.layout.GroupLayout(fourierControlPanel2);
        fourierControlPanel2.setLayout(fourierControlPanel2Layout);
        fourierControlPanel2Layout.setHorizontalGroup(
            fourierControlPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(fourierControlPanel2Layout.createSequentialGroup()
                .add(jButton7)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel3)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(icaComponentField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 43, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(fourierIcaCheckbox)
                .addContainerGap(78, Short.MAX_VALUE))
        );
        fourierControlPanel2Layout.setVerticalGroup(
            fourierControlPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(fourierControlPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                .add(jButton7)
                .add(jLabel3)
                .add(icaComponentField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(fourierIcaCheckbox))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("SVD ICA"));

        jButton8.setText("SVD ICA");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jButton3.setText("Redraw");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jButton8)
                .add(18, 18, 18)
                .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(21, 21, 21)
                .add(jButton3)
                .addContainerGap(111, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                .add(jButton8)
                .add(jButton3)
                .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );

        loadDelayedNetworkButton.setText("Load Delayed Network");
        loadDelayedNetworkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadDelayedNetworkButtonActionPerformed(evt);
            }
        });

        loadElmanNetworkButton.setText("Load Elman Network");
        loadElmanNetworkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadElmanNetworkButtonActionPerformed(evt);
            }
        });

        loadComputeFakeDataButton.setText("Load and Compute Fake Data");
        loadComputeFakeDataButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadComputeFakeDataButtonActionPerformed(evt);
            }
        });

        computeRealData.setText("Compute Real Data");
        computeRealData.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                computeRealDataActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout controlPanelLayout = new org.jdesktop.layout.GroupLayout(controlPanel);
        controlPanel.setLayout(controlPanelLayout);
        controlPanelLayout.setHorizontalGroup(
            controlPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(controlPanelLayout.createSequentialGroup()
                .add(controlPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(controlPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, fourierControlPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, fourierControlPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, fourierControlPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .add(controlPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .add(controlPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(loadDelayedNetworkButton)
                            .add(loadElmanNetworkButton)
                            .add(loadComputeFakeDataButton)
                            .add(computeRealData))))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        controlPanelLayout.setVerticalGroup(
            controlPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(controlPanelLayout.createSequentialGroup()
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(4, 4, 4)
                .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(fourierControlPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(fourierControlPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(fourierControlPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(loadDelayedNetworkButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(loadElmanNetworkButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(loadComputeFakeDataButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(computeRealData)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout graphPanelLayout = new org.jdesktop.layout.GroupLayout(graphPanel);
        graphPanel.setLayout(graphPanelLayout);
        graphPanelLayout.setHorizontalGroup(
            graphPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 1048, Short.MAX_VALUE)
        );
        graphPanelLayout.setVerticalGroup(
            graphPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 883, Short.MAX_VALUE)
        );

        ecgdatatab.addTab("ECG", graphPanel);

        fourierPanel.setVerifyInputWhenFocusTarget(false);

        org.jdesktop.layout.GroupLayout fourierPanelLayout = new org.jdesktop.layout.GroupLayout(fourierPanel);
        fourierPanel.setLayout(fourierPanelLayout);
        fourierPanelLayout.setHorizontalGroup(
            fourierPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 1048, Short.MAX_VALUE)
        );
        fourierPanelLayout.setVerticalGroup(
            fourierPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 883, Short.MAX_VALUE)
        );

        ecgdatatab.addTab("Fourier Transform", fourierPanel);

        org.jdesktop.layout.GroupLayout waveletPanelLayout = new org.jdesktop.layout.GroupLayout(waveletPanel);
        waveletPanel.setLayout(waveletPanelLayout);
        waveletPanelLayout.setHorizontalGroup(
            waveletPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 1048, Short.MAX_VALUE)
        );
        waveletPanelLayout.setVerticalGroup(
            waveletPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 883, Short.MAX_VALUE)
        );

        ecgdatatab.addTab("Wavelet transform", waveletPanel);

        org.jdesktop.layout.GroupLayout icaTabLayout = new org.jdesktop.layout.GroupLayout(icaTab);
        icaTab.setLayout(icaTabLayout);
        icaTabLayout.setHorizontalGroup(
            icaTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 1048, Short.MAX_VALUE)
        );
        icaTabLayout.setVerticalGroup(
            icaTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 883, Short.MAX_VALUE)
        );

        ecgdatatab.addTab("ICA", icaTab);

        org.jdesktop.layout.GroupLayout SVDpanelLayout = new org.jdesktop.layout.GroupLayout(SVDpanel);
        SVDpanel.setLayout(SVDpanelLayout);
        SVDpanelLayout.setHorizontalGroup(
            SVDpanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 1048, Short.MAX_VALUE)
        );
        SVDpanelLayout.setVerticalGroup(
            SVDpanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 883, Short.MAX_VALUE)
        );

        ecgdatatab.addTab("SVD ICA", SVDpanel);

        org.jdesktop.layout.GroupLayout nnPanelLayout = new org.jdesktop.layout.GroupLayout(nnPanel);
        nnPanel.setLayout(nnPanelLayout);
        nnPanelLayout.setHorizontalGroup(
            nnPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 1048, Short.MAX_VALUE)
        );
        nnPanelLayout.setVerticalGroup(
            nnPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 883, Short.MAX_VALUE)
        );

        ecgdatatab.addTab("Neural Networks", nnPanel);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(controlPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(ecgdatatab))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(10, 10, 10)
                .add(ecgdatatab))
            .add(controlPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        String currentDataPath = getCurrentDataPath((getClass().getProtectionDomain().getCodeSource().getLocation().toString()).substring(5));
        JFileChooser fileChooser = new JFileChooser(currentDataPath);
        boolean choose = true;
        boolean success = false;
        File file = null;
        while (choose) {
            int returnVal = fileChooser.showOpenDialog(this);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                file = fileChooser.getSelectedFile();
                String extension = getExtension(file.getName());
                //System.out.println("Read file with extension [" + extension + "]");
                if (extension.compareTo(".edf") == 0) {
                    //System.out.println("Read edf file " + file.getPath());
                    choose = false;
                    success = true;
                } else if (extension.compareTo(".txt") == 0) {
                    //System.out.println("Read txt file " + file.getPath());
                    choose = false;
                    success = true;
                }
            } else if (returnVal == JFileChooser.CANCEL_OPTION || returnVal == JFileChooser.ERROR_OPTION) {
                choose = false;
            }
        }
        if (success && file != null) {
            currentFile = file;
            ArrayList[] data;
            try {
                data = DataReader.read(file.getPath());
                createDataset(data);
                showSignals = pickDataset(getChecks());
                showECGGraph(currentFile, showSignals);
            } catch (IOException ex) {
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

        int[] minmax = getMinMax();
        try {
            if (graphPanel.isVisible()) {
                ChartPanel ecgPanel = (ChartPanel) graphPanel;
                ecgPanel.getChart().getXYPlot().getDomainAxis().setLowerBound(minmax[0]);
                ecgPanel.getChart().getXYPlot().getDomainAxis().setUpperBound(minmax[1]);
                ecgPanel.getChart().getXYPlot().setDataset(pickDataset(getChecks()));
            }
        } catch (ClassCastException e) {
            // There is no chart on the ecg panel
        }
        try {
            if (fourierPanel.isVisible()) {
                ChartPanel fPanel = (ChartPanel) fourierPanel;
                fPanel.getChart().getXYPlot().getDomainAxis().setLowerBound(minmax[0]);
                fPanel.getChart().getXYPlot().getDomainAxis().setUpperBound(minmax[1]);
                fPanel.getChart().getXYPlot().setDataset(pickFourierDataSet(getChecks()));
            }
        } catch (ClassCastException e) {
            // There is no chart on the fourier panel
        }
        try {
            if (waveletPanel.isVisible()) {
                ChartPanel wPanel = (ChartPanel) waveletPanel;
                wPanel.getChart().getXYPlot().getDomainAxis().setLowerBound(minmax[0]);
                wPanel.getChart().getXYPlot().getDomainAxis().setUpperBound(minmax[1]);
                wPanel.getChart().getXYPlot().setDataset(pickWaveletDataSet(getChecks()));
            }
        } catch (ClassCastException e) {
            // There is no chart on the fourier panel
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        if (allthorax_1 != null) {
            createFourierDataset();
            showFourierSignals = pickFourierDataSet(getChecks());
            showFourierGraph(currentFile, showFourierSignals);
        }
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        ChartPanel panel = (ChartPanel) graphPanel;
        panel.restoreAutoBounds();
}//GEN-LAST:event_jButton4ActionPerformed

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
    if (allthorax_1 != null) {
        createWaveletDataset();
        showWaveletSignals = pickWaveletDataSet(getChecks());
        showWaveletGraph(currentFile, showWaveletSignals);
    }
}//GEN-LAST:event_jButton6ActionPerformed

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
    if (allthorax_1 != null) {
        int noOfComponents = Integer.parseInt(icaComponentField.getText());
        double[][] s = getSignals();
        double[][] signals = null;
        double[] frequencies;
        if (fourierIcaCheckbox.isSelected()) {
            signals = new double[s.length][];
            
            for (int i = 0; i < s.length; i++) {
                ArrayList list = new ArrayList<Double>();
                list = new ArrayList(Arrays.asList(convertDoubles2(s[i])));
                Fourier fourier = new Fourier(list, 1000);
                signals[i] = new double[fourier.getAmplitude().size()];
                
                for (int j = 0; j < fourier.getAmplitude().size(); j++) {
                    signals[i][j] = fourier.getAmplitude().get(j);
                    if (i == 0) {
                        frequencies = new double[fourier.getFrequencies().size()];
                        frequencies[j] = fourier.getFrequencies().get(j);
                    }
                }

            }
        } else {
            signals = FastICA.fastICA(s, 50, 0.001, noOfComponents);
        }
        for (int i = 0; i < noOfComponents; i++) {
            XYSeries data = new XYSeries("ICA Component " + (i + 1));
            for (int j = 0; j < signals[i].length; j++) {
                if (fourierIcaCheckbox.isSelected()) {
                    data.add((double) alltimes[j], (double) signals[i][j]);
                } else {
                    data.add((double) alltimes[j], (double) signals[i][j]);
                }
            }
            XYSeriesCollection dataset = new XYSeriesCollection(data);

            JFreeChart chart = ChartFactory.createXYLineChart(
                    "ICA Component " + (i + 1),
                    // chart title
                    "",
                    // domain axis label
                    "",
                    // range axis label
                    dataset,
                    // data
                    PlotOrientation.VERTICAL,
                    // orientation
                    true,
                    // include legend
                    true,
                    // tooltips
                    false // urls
                    );
            ChartFrame frame = new ChartFrame("ICA Component " + (i + 1), chart);
            frame.setSize(800, 600);
            frame.setVisible(true);
        }
        //showICAGraph(currentFile, pickICADataSet(getChecks(), signals));
    }
}//GEN-LAST:event_jButton7ActionPerformed

private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
    if (allthorax_1 != null) {

        createSVDICADataset();
        showSVDICASignals = pickSVDICADataSet(getChecks());
        showSVDICAGraph(currentFile, showSVDICASignals);
    }
}//GEN-LAST:event_jButton8ActionPerformed

private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_jComboBox1ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
    if (allthorax_1 != null) {

        showSVDICASignals = pickSVDICADataSet(getChecks());
        showSVDICAGraph(currentFile, showSVDICASignals);
    }
}//GEN-LAST:event_jButton3ActionPerformed

private void icaComponentFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_icaComponentFieldActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_icaComponentFieldActionPerformed

private void fourierIcaCheckboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fourierIcaCheckboxActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_fourierIcaCheckboxActionPerformed

private void loadDelayedNetworkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadDelayedNetworkButtonActionPerformed
    // TODO add your handling code here:
    File file;
    JFileChooser chooser = new JFileChooser();
    int ret;

     ret = chooser.showDialog(null, "Open file");

     if (ret == JFileChooser.APPROVE_OPTION)
     {
        file = chooser.getSelectedFile();

        BasicNetwork bla = (BasicNetwork) EncogDirectoryPersistence.loadObject(new File(file.getAbsolutePath()));
        delayedNetwork = new DelayedNetwork(bla);
     }
}//GEN-LAST:event_loadDelayedNetworkButtonActionPerformed

private void loadElmanNetworkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadElmanNetworkButtonActionPerformed
    // TODO add your handling code here:
    File file;
    JFileChooser chooser = new JFileChooser();
    int ret;

     ret = chooser.showDialog(null, "Open file");

     if (ret == JFileChooser.APPROVE_OPTION)
     {
        file = chooser.getSelectedFile();

        BasicNetwork bla = (BasicNetwork) EncogDirectoryPersistence.loadObject(new File(file.getAbsolutePath()));
        elmanNetwork = new ElmanNetwork(bla);
     }
}//GEN-LAST:event_loadElmanNetworkButtonActionPerformed

private void loadComputeFakeDataButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadComputeFakeDataButtonActionPerformed
    // TODO add your handling code here:
    File file;
    JFileChooser chooser = new JFileChooser();
    int ret;

    ret = chooser.showDialog(null, "Open file");

    if (ret == JFileChooser.APPROVE_OPTION)
    {
        file = chooser.getSelectedFile();
        try
        {
            fakeData = DataReader.readTrainingData(file.getAbsolutePath());
        }
        catch (IOException ex)
        {
            Logger.getLogger(Trainer.class.getName()).log(Level.SEVERE, null, ex);
         }
     }

     if(fakeData.length != 2){
         fakeData = Arrays.copyOfRange(fakeData, 2,4);
     }
     
     if(fakeData.length != 2){
         System.out.println("Error, because the fakeData array is not of good length");
     }
     
     //use wavelet low-pass filter method
     fakeData[0] = Wavelet.waveletFilter(fakeData[0]);
        
    if(delayedNetwork == null ){
        if(elmanNetwork != null){

            allElmanIdeal = fakeData[1].toArray(new Double[0]);
            allElmanOriginal = fakeData[0].toArray(new Double[0]);
            allElmanFake = elmanNetwork.computeSet(fakeData[0].toArray(new Double[0]));

            allFakeTimes = new Double[fakeData[0].size()];
            for(double i = 0; i < fakeData[0].size(); i++){
                allFakeTimes[(int) i] = (i/1000);
                //System.out.println("i: " + i + " tijd: " + (i/fakeData[0].size())*60);
            }
//            for(double d: allElmanFake){
//            System.out.println("computed by elman fake: " + d);
//            }

            XYSeriesCollection col = new XYSeriesCollection();
            XYSeries elmanFake = new XYSeries("elmanFake");
            XYSeries elmanIdeal = new XYSeries("elmanIdeal");
            XYSeries elmanOriginal = new XYSeries("elmanOriginal");

            for (int i = 0; i < allElmanFake.length; i++) {
                elmanFake.add(allFakeTimes[i], allElmanFake[i]);
                elmanIdeal.add(allFakeTimes[i], allElmanIdeal[i]);
                elmanOriginal.add(allFakeTimes[i], allElmanOriginal[i]);
            }
            col.addSeries(elmanFake);
            col.addSeries(elmanIdeal);
            col.addSeries(elmanOriginal);

            showNNGraph(currentFile, col);
            double[] morph = Morphology.morphology(allElmanIdeal, allElmanFake);
            System.out.println("Morphology, average difference: " + morph[0] + ", max dif: " + morph[1] + ", min dif: " + morph[2]);
        }else{
            System.out.println("No network loaded!!");
        }
    } else{
        allDelayedIdeal = fakeData[1].toArray(new Double[0]);
        allDelayedOriginal = fakeData[0].toArray(new Double[0]);
        allDelayedFake = delayedNetwork.computeSet(fakeData[0].toArray(new Double[0]));
//        for(double d: allDelayedFake){
//            System.out.println("computed by delayed fake: " + d);
//        }

        allFakeTimes = new Double[fakeData[0].size()];
        for(double i = 0; i < fakeData[0].size(); i++){
            allFakeTimes[(int) i] = (i/1000);
            //System.out.println("i: " + i + " tijd: " + (i/fakeData[0].size())*60);
        }

        XYSeriesCollection col = new XYSeriesCollection();
        XYSeries delayedFake = new XYSeries("delayedFake");
        XYSeries delayedIdeal = new XYSeries("delayedIdeal");
        XYSeries delayedOriginal = new XYSeries("delayedOriginal");

        for (int i = 0; i < allDelayedFake.length; i++) {
            delayedFake.add(allFakeTimes[i], allDelayedFake[i]);
            delayedIdeal.add(allFakeTimes[i], allDelayedIdeal[i]);
            delayedOriginal.add(allFakeTimes[i], allDelayedOriginal[i]);
        }
        col.addSeries(delayedFake);
        col.addSeries(delayedIdeal);
        col.addSeries(delayedOriginal);

        showNNGraph(currentFile, col);
        double[] morph = Morphology.morphology(allDelayedIdeal, allDelayedFake);
        System.out.println("Morphology, average difference: " + morph[0] + ", max dif: " + morph[1] + ", min dif: " + morph[2]);
    }
}//GEN-LAST:event_loadComputeFakeDataButtonActionPerformed

private void computeRealDataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_computeRealDataActionPerformed
    // TODO add your handling code here:
    if (allthorax_1 != null) {
        createNNDataset();
        showNNSignals = pickNNDataSet(getChecks());
        showNNGraph(currentFile, showNNSignals);
    }
}//GEN-LAST:event_computeRealDataActionPerformed

    private void loadFakeDataButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadFakeDataButtonActionPerformed
        // TODO add your handling code here:
        String currentDataPath = getCurrentDataPath((getClass().getProtectionDomain().getCodeSource().getLocation().toString()).substring(5));
        JFileChooser fileChooser = new JFileChooser(currentDataPath);
        boolean choose = true;
        boolean success = false;
        File file = null;
        while (choose) {
            int returnVal = fileChooser.showOpenDialog(this);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                file = fileChooser.getSelectedFile();
                String extension = getExtension(file.getName());
                //System.out.println("Read file with extension [" + extension + "]");
                if (extension.compareTo(".edf") == 0) {
                    //System.out.println("Read edf file " + file.getPath());
                    choose = false;
                    success = true;
                } else if (extension.compareTo(".txt") == 0) {
                    //System.out.println("Read txt file " + file.getPath());
                    choose = false;
                    success = true;
                }
            } else if (returnVal == JFileChooser.CANCEL_OPTION || returnVal == JFileChooser.ERROR_OPTION) {
                choose = false;
            }
        }
        if (success && file != null) {
            currentFile = file;
            ArrayList[] data;
            try {
                data = DataReader.readFake(file.getPath());
                createDataset(data);
                showSignals = pickDataset(getChecks());
                showECGGraph(currentFile, showSignals);
            } catch (IOException ex) {
            }
        }
    }//GEN-LAST:event_loadFakeDataButtonActionPerformed

private void showNNGraph(File file, XYDataset dataset) {

        JFreeChart chart = ChartFactory.createXYLineChart(
                "Neural Network ",
                // chart title
                "NN x-axis",
                // domain axis label
                "NN y-axis",
                // range axis label
                dataset,
                // data
                PlotOrientation.VERTICAL,
                // orientation
                true,
                // include legend
                true,
                // tooltips
                false // urls
                );
        ecgdatatab.remove(nnPanel);
        nnPanel = new ChartPanel(chart);
        nnPanel.setBounds(0, 0, ecgdatatab.getWidth() - 20, ecgdatatab.getHeight() - 20);
        nnPanel.setName("Neural Network");
        ecgdatatab.add(nnPanel, 2);
        ecgdatatab.setSelectedComponent(nnPanel);
        setSize(this.getWidth() + 1, this.getHeight());
        setSize(this.getWidth() - 1, this.getHeight());
    }

private void createNNDataset() {
        if(delayedNetwork == null && elmanNetwork != null){
            allNNthorax_1 = elmanNetwork.computeSet(Wavelet.waveletFilter(allthorax_1));
            allNNthorax_2 = elmanNetwork.computeSet(Wavelet.waveletFilter(allthorax_2));
            allNNabdomen_1 = elmanNetwork.computeSet(Wavelet.waveletFilter(allabdomen_1));
            allNNabdomen_2 = elmanNetwork.computeSet(Wavelet.waveletFilter(allabdomen_2));
            allNNabdomen_3 = elmanNetwork.computeSet(Wavelet.waveletFilter(allabdomen_3));
        } else if(delayedNetwork != null){
            allNNthorax_1 = delayedNetwork.computeSet(Wavelet.waveletFilter(allthorax_1));
            allNNthorax_2 = delayedNetwork.computeSet(Wavelet.waveletFilter(allthorax_2));
            allNNabdomen_1 = delayedNetwork.computeSet(Wavelet.waveletFilter(allabdomen_1));
            allNNabdomen_2 = delayedNetwork.computeSet(Wavelet.waveletFilter(allabdomen_2));
            allNNabdomen_3 = delayedNetwork.computeSet(Wavelet.waveletFilter(allabdomen_3));
        } else {
            System.out.println("No network selected!!!!");
        }
    }


    private XYSeriesCollection pickNNDataSet(boolean[] checks) {

        XYSeriesCollection col = new XYSeriesCollection();

        XYSeries thorax_1 = new XYSeries("thorax_1");
        XYSeries thorax_2 = new XYSeries("thorax_2");
        XYSeries abdomen_1 = new XYSeries("abdomen_1");
        XYSeries abdomen_2 = new XYSeries("abdomen_2");
        XYSeries abdomen_3 = new XYSeries("abdomen_3");

        for (int i = 0; i < allNNthorax_1.length; i++) {
            thorax_1.add(alltimes[i], allNNthorax_1[i]);
            thorax_2.add(alltimes[i], allNNthorax_2[i]);
            abdomen_1.add(alltimes[i], allNNabdomen_1[i]);
            abdomen_2.add(alltimes[i], allNNabdomen_2[i]);
            abdomen_3.add(alltimes[i], allNNabdomen_3[i]);
        }
        if (checks[0]) {
            col.addSeries(thorax_1);
        }
        if (checks[1]) {
            col.addSeries(thorax_2);
        }
        if (checks[2]) {
            col.addSeries(abdomen_1);
        }
        if (checks[3]) {
            col.addSeries(abdomen_2);
        }
        if (checks[4]) {
            col.addSeries(abdomen_3);
        }
        return col;
    }

    private void showSVDICAGraph(File file, XYDataset dataset) {

        JFreeChart chart = ChartFactory.createXYLineChart(
                "SVD ICA " + file.getName(),
                // chart title
                "time (s)",
                // domain axis label
                "amplitude()",
                // range axis label
                dataset,
                // data
                PlotOrientation.VERTICAL,
                // orientation
                true,
                // include legend
                true,
                // tooltips
                false // urls
                );
        ecgdatatab.remove(SVDpanel);
        SVDpanel = new ChartPanel(chart);
        SVDpanel.setBounds(0, 0, ecgdatatab.getWidth() - 20, ecgdatatab.getHeight() - 20);
        SVDpanel.setName("SVD ICA");
        ecgdatatab.add(SVDpanel, 1);
        ecgdatatab.setSelectedComponent(SVDpanel);
        setSize(this.getWidth() + 1, this.getHeight());
        setSize(this.getWidth() - 1, this.getHeight());
    }

    /**
     * not sure what should be on the X-axis, I think time,
     * And I'm currently only plotting 1 of the results instead of all 10.
     * Eventually we will do this anyway sinc we will figure out which of the 10
     * is the correct one
     * @param checks
     * @return
     */
    private XYSeriesCollection pickSVDICADataSet(boolean[] checks) {

        XYSeriesCollection col = new XYSeriesCollection();

        XYSeries thorax_1 = new XYSeries("thorax_1");
        XYSeries thorax_2 = new XYSeries("thorax_2");
        XYSeries abdomen_1 = new XYSeries("abdomen_1");
        XYSeries abdomen_2 = new XYSeries("abdomen_2");
        XYSeries abdomen_3 = new XYSeries("abdomen_3");

        //It now draws the first sets instead of the best looking for Mother and child

        for (int i = 0; i < allSVDthorax_1[0].length; i++) {
            thorax_1.add((double)i * 60.0/allSVDthorax_1[0].length, (Double) allSVDthorax_1[jComboBox1.getSelectedIndex()][i]);
            thorax_2.add((double)i* 60.0/allSVDthorax_1[0].length, (Double) allSVDthorax_2[jComboBox1.getSelectedIndex()][i]);
            abdomen_1.add((double)i* 60.0/allSVDthorax_1[0].length, (Double) allSVDabdomen_1[jComboBox1.getSelectedIndex()][i]);
            abdomen_2.add((double)i* 60.0/allSVDthorax_1[0].length, (Double) allSVDabdomen_2[jComboBox1.getSelectedIndex()][i]);
            abdomen_3.add((double)i* 60.0/allSVDthorax_1[0].length, (Double) allSVDabdomen_3[jComboBox1.getSelectedIndex()][i]);
        }
        if (checks[0]) {
            col.addSeries(thorax_1);
        }
        if (checks[1]) {
            col.addSeries(thorax_2);
        }
        if (checks[2]) {
            col.addSeries(abdomen_1);
        }
        if (checks[3]) {
            col.addSeries(abdomen_2);
        }
        if (checks[4]) {
            col.addSeries(abdomen_3);
        }

         XYSeries thorax_1b = new XYSeries("thorax_1");
        XYSeries thorax_2b = new XYSeries("thorax_2");
        XYSeries abdomen_1b = new XYSeries("abdomen_1");
        XYSeries abdomen_2b = new XYSeries("abdomen_2");
        XYSeries abdomen_3b = new XYSeries("abdomen_3");

        for (int i = 0; i < allthorax_1.length; i++) {
            thorax_1b.add(alltimes[i], allthorax_1[i]);
            thorax_2b.add(alltimes[i], allthorax_2[i]);
            abdomen_1b.add(alltimes[i], allabdomen_1[i]);
            abdomen_2b.add(alltimes[i], allabdomen_2[i]);
            abdomen_3b.add(alltimes[i], allabdomen_3[i]);
        }
        if (checks[0]) {
            col.addSeries(thorax_1b);
        }
        if (checks[1]) {
            col.addSeries(thorax_2b);
        }
        if (checks[2]) {
            col.addSeries(abdomen_1b);
        }
        if (checks[3]) {
            col.addSeries(abdomen_2b);
        }
        if (checks[4]) {
            col.addSeries(abdomen_3b);
        }
        return col;
    }

    private void createSVDICADataset() {
        SVDICA thorax1SVD = new SVDICA(new ArrayList(Arrays.asList(allthorax_1)), 1000.0);
        SVDICA thorax2SVD = new SVDICA(new ArrayList(Arrays.asList(allthorax_2)), 1000.0);
        SVDICA abdomen1SVD = new SVDICA(new ArrayList(Arrays.asList(allabdomen_1)), 1000.0);
        SVDICA abdomen2SVD = new SVDICA(new ArrayList(Arrays.asList(allabdomen_2)), 1000.0);
        SVDICA abdomen3SVD = new SVDICA(new ArrayList(Arrays.asList(allabdomen_3)), 1000.0);

        System.out.println("done one");
        allSVDthorax_1 = thorax1SVD.getICAU();
        System.out.println("done one2");
        allSVDthorax_2 = thorax2SVD.getICAU();
        System.out.println("done one3");
        allSVDabdomen_1 = abdomen1SVD.getICAU();
        System.out.println("done one4");
        allSVDabdomen_2 = abdomen2SVD.getICAU();
        System.out.println("done one5");
        allSVDabdomen_3 = abdomen3SVD.getICAU();
        System.out.println("done one6");
    }

    private double[] convertDoubles(Double[] doubles) {
        double[] result = new double[doubles.length];
        for (int i = 0; i < doubles.length; i++) {
            result[i] = doubles[i];
        }
        return result;
    }
    
    private Double[] convertDoubles2(double[] doubles) {
        Double[] result = new Double[doubles.length];
        for (int i = 0; i < doubles.length; i++) {
            result[i] = doubles[i];
        }
        return result;
    }
     

    private double[][] getSignals() {

        double[][] signals = new double[getNumberChecks()][allthorax_1.length];

        for (int i = 0; i < allthorax_1.length; i++) {
            int fill = 0;
            if (thorax_1_checkbox.isSelected()) {
                signals[fill][i] = allthorax_1[i];
                fill++;
            }
            if (thorax_2_checkbox.isSelected()) {
                signals[fill][i] = allthorax_2[i];
                fill++;
            }
            if (abdomen_1_checkbox.isSelected()) {
                signals[fill][i] = allabdomen_1[i];
                fill++;
            }
            if (abdomen_2_checkbox.isSelected()) {
                signals[fill][i] = allabdomen_2[i];
                fill++;
            }
            if (abdomen_3_checkbox.isSelected()) {
                signals[fill][i] = allabdomen_3[i];
                fill++;
            }
        }
        System.out.println(getNumberChecks() + " signals chosen");
        return signals;
    }

    private int getNumberChecks() {
        boolean[] checks = getChecks();
        int number = 0;
        for (int i = 0; i < checks.length; i++) {
            if (checks[i]) {
                number++;
            }
        }
        return number;
    }

    private void showECGGraph(File file, XYDataset dataset) {

        JFreeChart chart = ChartFactory.createXYLineChart(
                "ECG " + file.getName(),
                // chart title
                "time(s)",
                // domain axis label
                "voltage(V)",
                // range axis label
                dataset,
                // data
                PlotOrientation.VERTICAL,
                // orientation
                true,
                // include legend
                true,
                // tooltips
                false // urls
                );
        ecgdatatab.remove(graphPanel);
        graphPanel = new ChartPanel(chart);
        graphPanel.setBounds(0, 0, ecgdatatab.getWidth() - 20, ecgdatatab.getHeight() - 20);
        graphPanel.setName("ECG");
        ecgdatatab.add(graphPanel, 0);
        ecgdatatab.setSelectedComponent(graphPanel);
        setSize(this.getWidth() + 1, this.getHeight());
        setSize(this.getWidth() - 1, this.getHeight());
        minTextfield.setText("" + chart.getXYPlot().getDomainAxis().getLowerBound());
        maxTextfield.setText("" + chart.getXYPlot().getDomainAxis().getUpperBound());
    }

    private void showFourierGraph(File file, XYDataset dataset) {

        JFreeChart chart = ChartFactory.createXYLineChart(
                "Fourier Transform " + file.getName(),
                // chart title
                "frequency(Hz)",
                // domain axis label
                "amplitude()",
                // range axis label
                dataset,
                // data
                PlotOrientation.VERTICAL,
                // orientation
                true,
                // include legend
                true,
                // tooltips
                false // urls
                );
        ecgdatatab.remove(fourierPanel);
        fourierPanel = new ChartPanel(chart);
        fourierPanel.setBounds(0, 0, ecgdatatab.getWidth() - 20, ecgdatatab.getHeight() - 20);
        fourierPanel.setName("Fourier Transform");
        ecgdatatab.add(fourierPanel, 1);
        ecgdatatab.setSelectedComponent(fourierPanel);
        setSize(this.getWidth() + 1, this.getHeight());
        setSize(this.getWidth() - 1, this.getHeight());
    }

    private void showWaveletGraph(File file, XYDataset dataset) {

        JFreeChart chart = ChartFactory.createXYLineChart(
                "Wavelet Transform " + file.getName(),
                // chart title
                "Wavelet x-axis",
                // domain axis label
                "Wavelet y-axis",
                // range axis label
                dataset,
                // data
                PlotOrientation.VERTICAL,
                // orientation
                true,
                // include legend
                true,
                // tooltips
                false // urls
                );
        ecgdatatab.remove(waveletPanel);
        waveletPanel = new ChartPanel(chart);
        waveletPanel.setBounds(0, 0, ecgdatatab.getWidth() - 20, ecgdatatab.getHeight() - 20);
        waveletPanel.setName("Wavelet Transform");
        ecgdatatab.add(waveletPanel, 2);
        ecgdatatab.setSelectedComponent(waveletPanel);
        setSize(this.getWidth() + 1, this.getHeight());
        setSize(this.getWidth() - 1, this.getHeight());
    }

    private void showICAGraph(File file, XYDataset dataset) {

        JFreeChart chart = ChartFactory.createXYLineChart(
                "ICA " + file.getName(),
                // chart title
                "ICA x-axis",
                // domain axis label
                "ICA y-axis",
                // range axis label
                dataset,
                // data
                PlotOrientation.VERTICAL,
                // orientation
                true,
                // include legend
                true,
                // tooltips
                false // urls
                );
        ecgdatatab.remove(icaTab);
        icaTab = new ChartPanel(chart);
        icaTab.setBounds(0, 0, ecgdatatab.getWidth() - 20, ecgdatatab.getHeight() - 20);
        icaTab.setName("ICA");
        ecgdatatab.add(icaTab, 3);
        ecgdatatab.setSelectedComponent(icaTab);
        setSize(this.getWidth() + 1, this.getHeight());
        setSize(this.getWidth() - 1, this.getHeight());
    }

    private boolean[] getChecks() {
        return new boolean[]{
                    thorax_1_checkbox.isSelected(),
                    thorax_2_checkbox.isSelected(),
                    abdomen_1_checkbox.isSelected(),
                    abdomen_2_checkbox.isSelected(),
                    abdomen_3_checkbox.isSelected()
                };
    }

    private int[] getMinMax() {
        int maxBound = 60;
        if (fourierPanel.isVisible()) {
            maxBound = 1000;
        } else if (waveletPanel.isVisible()) {
            maxBound = 1000;
        }
        int min = tryParse(minTextfield.getText()) ? Integer.parseInt(minTextfield.getText()) : 0;
        int max = tryParse(maxTextfield.getText()) ? Integer.parseInt(maxTextfield.getText()) : maxBound;
        return new int[]{min, max};

    }

    public static boolean tryParse(String text) {
        try {
            int x = new Integer(text);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    private void createDataset(ArrayList[] data) {
        allthorax_1 = new Double[data[0].size()];
        allthorax_2 = new Double[data[0].size()];
        allabdomen_1 = new Double[data[0].size()];
        allabdomen_2 = new Double[data[0].size()];
        allabdomen_3 = new Double[data[0].size()];
        allannotations = new Double[data[0].size()];
        alltimes = new Double[data[0].size()];
        for (int i = 0; i < data[0].size(); i++) {
            alltimes[i] = Double.parseDouble(data[0].get(i).toString());
            allthorax_1[i] = Double.parseDouble(data[1].get(i).toString());
            allthorax_2[i] = Double.parseDouble(data[2].get(i).toString());
            allabdomen_1[i] = Double.parseDouble(data[3].get(i).toString());
            allabdomen_2[i] = Double.parseDouble(data[4].get(i).toString());
            allabdomen_3[i] = Double.parseDouble(data[5].get(i).toString());
            allannotations[i] = Double.parseDouble(data[6].get(i).toString());
            
        }
    }

    private void createFourierDataset() {
        Fourier thorax1Transform = new Fourier(new ArrayList(Arrays.asList(allthorax_1)), 1000.0);
        Fourier thorax2Transform = new Fourier(new ArrayList(Arrays.asList(allthorax_2)), 1000.0);
        Fourier abdomen1Transform = new Fourier(new ArrayList(Arrays.asList(allabdomen_1)), 1000.0);
        Fourier abdomen2Transform = new Fourier(new ArrayList(Arrays.asList(allabdomen_2)), 1000.0);
        Fourier abdomen3Transform = new Fourier(new ArrayList(Arrays.asList(allabdomen_3)), 1000.0);
        allFourierfrequencies = thorax1Transform.getFrequencies().toArray(new Double[0]);
        allFourierthorax_1 = thorax1Transform.getAmplitude().toArray(new Double[0]);
        allFourierthorax_2 = thorax2Transform.getAmplitude().toArray(new Double[0]);
        allFourierabdomen_1 = abdomen1Transform.getAmplitude().toArray(new Double[0]);
        allFourierabdomen_2 = abdomen2Transform.getAmplitude().toArray(new Double[0]);
        allFourierabdomen_3 = abdomen3Transform.getAmplitude().toArray(new Double[0]);
    }

    private void createWaveletDataset() {
        allWaveletthorax_1 = Wavelet.waveletFilter(new ArrayList(Arrays.asList(allthorax_1))).toArray(new Double[0]);
        allWaveletthorax_2 = Wavelet.waveletFilter(new ArrayList(Arrays.asList(allthorax_2))).toArray(new Double[0]);
        allWaveletabdomen_1 = Wavelet.waveletFilter(new ArrayList(Arrays.asList(allabdomen_1))).toArray(new Double[0]);
        allWaveletabdomen_2 = Wavelet.waveletFilter(new ArrayList(Arrays.asList(allabdomen_2))).toArray(new Double[0]);
        allWaveletabdomen_3 = Wavelet.waveletFilter(new ArrayList(Arrays.asList(allabdomen_3))).toArray(new Double[0]);
    }

    private XYSeriesCollection pickDataset(boolean[] checks) {

        XYSeriesCollection col = new XYSeriesCollection();

        XYSeries thorax_1 = new XYSeries("thorax_1");
        XYSeries thorax_2 = new XYSeries("thorax_2");
        XYSeries abdomen_1 = new XYSeries("abdomen_1");
        XYSeries abdomen_2 = new XYSeries("abdomen_2");
        XYSeries abdomen_3 = new XYSeries("abdomen_3");

        for (int i = 0; i < allthorax_1.length; i++) {
            thorax_1.add(alltimes[i], allthorax_1[i]);
            thorax_2.add(alltimes[i], allthorax_2[i]);
            abdomen_1.add(alltimes[i], allabdomen_1[i]);
            abdomen_2.add(alltimes[i], allabdomen_2[i]);
            abdomen_3.add(alltimes[i], allabdomen_3[i]);
        }
        if (checks[0]) {
            col.addSeries(thorax_1);
        }
        if (checks[1]) {
            col.addSeries(thorax_2);
        }
        if (checks[2]) {
            col.addSeries(abdomen_1);
        }
        if (checks[3]) {
            col.addSeries(abdomen_2);
        }
        if (checks[4]) {
            col.addSeries(abdomen_3);
        }
        return col;
    }

    private XYSeriesCollection pickFourierDataSet(boolean[] checks) {

        XYSeriesCollection col = new XYSeriesCollection();

        XYSeries thorax_1 = new XYSeries("thorax_1");
        XYSeries thorax_2 = new XYSeries("thorax_2");
        XYSeries abdomen_1 = new XYSeries("abdomen_1");
        XYSeries abdomen_2 = new XYSeries("abdomen_2");
        XYSeries abdomen_3 = new XYSeries("abdomen_3");

        for (int i = 0; i < allFourierthorax_1.length; i++) {
            thorax_1.add(allFourierfrequencies[i], allFourierthorax_1[i]);
            thorax_2.add(allFourierfrequencies[i], allFourierthorax_2[i]);
            abdomen_1.add(allFourierfrequencies[i], allFourierabdomen_1[i]);
            abdomen_2.add(allFourierfrequencies[i], allFourierabdomen_2[i]);
            abdomen_3.add(allFourierfrequencies[i], allFourierabdomen_3[i]);
        }
        if (checks[0]) {
            col.addSeries(thorax_1);
        }
        if (checks[1]) {
            col.addSeries(thorax_2);
        }
        if (checks[2]) {
            col.addSeries(abdomen_1);
        }
        if (checks[3]) {
            col.addSeries(abdomen_2);
        }
        if (checks[4]) {
            col.addSeries(abdomen_3);
        }
        return col;
    }

    private XYSeriesCollection pickWaveletDataSet(boolean[] checks) {

        XYSeriesCollection col = new XYSeriesCollection();

        XYSeries thorax_1 = new XYSeries("thorax_1");
        XYSeries thorax_2 = new XYSeries("thorax_2");
        XYSeries abdomen_1 = new XYSeries("abdomen_1");
        XYSeries abdomen_2 = new XYSeries("abdomen_2");
        XYSeries abdomen_3 = new XYSeries("abdomen_3");

        for (int i = 0; i < allWaveletthorax_1.length; i++) {
            thorax_1.add(alltimes[i], allWaveletthorax_1[i]);
            thorax_2.add(alltimes[i], allWaveletthorax_2[i]);
            abdomen_1.add(alltimes[i], allWaveletabdomen_1[i]);
            abdomen_2.add(alltimes[i], allWaveletabdomen_2[i]);
            abdomen_3.add(alltimes[i], allWaveletabdomen_3[i]);
        }
        if (checks[0]) {
            col.addSeries(thorax_1);
        }
        if (checks[1]) {
            col.addSeries(thorax_2);
        }
        if (checks[2]) {
            col.addSeries(abdomen_1);
        }
        if (checks[3]) {
            col.addSeries(abdomen_2);
        }
        if (checks[4]) {
            col.addSeries(abdomen_3);
        }
        return col;
    }

    private XYSeriesCollection pickICADataSet(boolean[] checks, double[][] signals) {

        XYSeriesCollection col = new XYSeriesCollection();

        XYSeries thorax_1 = new XYSeries("thorax_1");
        XYSeries thorax_2 = new XYSeries("thorax_2");
        XYSeries abdomen_1 = new XYSeries("abdomen_1");
        XYSeries abdomen_2 = new XYSeries("abdomen_2");
        XYSeries abdomen_3 = new XYSeries("abdomen_3");

        for (int i = 0; i < signals[0].length; i++) {
            thorax_1.add((double) alltimes[i], signals[0][i]);
            thorax_2.add((double) alltimes[i], signals[1][i]);
            abdomen_1.add((double) alltimes[i], signals[2][i]);
            abdomen_2.add((double) alltimes[i], signals[3][i]);
            abdomen_3.add((double) alltimes[i], signals[4][i]);
        }
        if (checks[0]) {
            col.addSeries(thorax_1);
        }
        if (checks[1]) {
            col.addSeries(thorax_2);
        }
        if (checks[2]) {
            col.addSeries(abdomen_1);
        }
        if (checks[3]) {
            col.addSeries(abdomen_2);
        }
        if (checks[4]) {
            col.addSeries(abdomen_3);
        }
        return col;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new GUI().setVisible(true);
            }
        });
    }

    private String getExtension(String fileName) {
        int dotPos = fileName.lastIndexOf(".");
        return fileName.substring(dotPos);
    }

    private String getCurrentDataPath(String path) {
        for (int i = 0; i < 3; i++) {
            int dotPos = path.lastIndexOf("/");
            path = path.substring(0, dotPos);
        }
        return path + "/src/TheBeatGoesOn/Data";
    }

    private String getCurrentDataReaderPath(String path) {
        for (int i = 0; i < 3; i++) {
            int dotPos = path.lastIndexOf("/");
            path = path.substring(0, dotPos);
        }
        return path + "/src/TheBeatGoesOn/DataReader";
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel SVDpanel;
    private javax.swing.JCheckBox abdomen_1_checkbox;
    private javax.swing.JCheckBox abdomen_2_checkbox;
    private javax.swing.JCheckBox abdomen_3_checkbox;
    private javax.swing.JButton computeRealData;
    private javax.swing.JPanel controlPanel;
    private javax.swing.JTabbedPane ecgdatatab;
    private javax.swing.JPanel fourierControlPanel;
    private javax.swing.JPanel fourierControlPanel1;
    private javax.swing.JPanel fourierControlPanel2;
    private javax.swing.JCheckBox fourierIcaCheckbox;
    private javax.swing.JPanel fourierPanel;
    private javax.swing.JPanel graphPanel;
    private javax.swing.JTextField icaComponentField;
    private javax.swing.JPanel icaTab;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JButton loadComputeFakeDataButton;
    private javax.swing.JButton loadDelayedNetworkButton;
    private javax.swing.JButton loadElmanNetworkButton;
    private javax.swing.JButton loadFakeDataButton;
    private javax.swing.JTextField maxTextfield;
    private javax.swing.JTextField minTextfield;
    private javax.swing.JPanel nnPanel;
    private javax.swing.JCheckBox thorax_1_checkbox;
    private javax.swing.JCheckBox thorax_2_checkbox;
    private javax.swing.JPanel waveletPanel;
    // End of variables declaration//GEN-END:variables
}
